#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson
from model import TopicM
from model import LectureM

class getTopicsHandler(webapp.RequestHandler):
    def get(self):
        lectureKey = self.request.get("lectureKey")
        if lectureKey:
            lecture=(LectureM.Lecture).get(lectureKey)
            result = TopicM.getTopicsByLecture(lecture).fetch(limit=100)
                
            if isinstance(result, basestring): #Error
                self.response.out.write(result)
            elif result:
                tOutput=[]
                for t in result:
                    temp=dict(name=t.name,key=str(t.key()))
                    tOutput.append(temp)
                    
                self.response.out.write(simplejson.dumps(tOutput))
        else:
            pass

application = webapp.WSGIApplication([(r'.*', getTopicsHandler)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()